﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrmContacto.Contracts.Contracts;
using Web.CrmContacto.Code;
using Web.CrmContacto.Code.Security;
using Web.Framework;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework.Translate;
using CrmContacto.WebApp.Code.InnerClasses;

namespace CrmContacto.WebApp.App
{
    public partial class SubPuntoDeVentaEdit : BasePage
    {

        private SubPuntoDeVenta SubPuntoDeVentaCurrent
        {
            get
            {
                return (SubPuntoDeVenta)ViewState["SubPuntoDeVentaCurrent"];
            }
            set
            {
                ViewState["SubPuntoDeVentaCurrent"] = value;
            }
        }

        private PuntoDeVenta PuntoDeVentaCurrent
        {
            get
            {
                return (PuntoDeVenta)ViewState["PuntoDeVentaCurrent"];
            }
            set
            {
                ViewState["PuntoDeVentaCurrent"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton eliminarButton = new LinkButton();
        LinkButton guardarButton = new LinkButton();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);


            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            LinkButton volverButton = new LinkButton();
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.CausesValidation = false;
            volverButton.ID = "BoCommandVolver";
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);

            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            base.LoadContextActions(actionButtons);
        }        

        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                HasPermissionToSeeMe(CrmSecurity.PDV_SEARCH, CrmSecurity.PDV_MANAGE);

                if (!this.IsPostBack)
                {
                    int pdvId = Convert.ToInt32(this.Request.QueryString["pdvId"]);
                    PuntoDeVentaCurrent = WebServiceHelper.GetPuntoDeVentaDto(new PuntoDeVentaSearch() { EmpresaId = BasePage.CompanyId , PuntoDeVentaId = pdvId, LoadSubPuntos = true }).First();
                    if (this.Request.QueryString["Id"] == null)
                    {
                        NuevoSubPuntoDeVenta(pdvId);
                    }
                    else
                    {
                        int subPtoVta = Convert.ToInt32(this.Request.QueryString["Id"]);                        
                        PuntoDeVentaCurrent.SubPuntoDeVentaIdActual = subPtoVta;
                        SubPuntoDeVentaCurrent = PuntoDeVentaCurrent.SubPuntoDeVentaActual;
                    }

                    Bind();
                }

            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }            
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            guardarButton.Visible =
            lnkGuardar.Visible = HasPermissionFor(CrmSecurity.PDV_MANAGE);

            if (SubPuntoDeVentaCurrent.Id != 0)
            {
                eliminarButton.Visible = HasPermissionFor(CrmSecurity.PDV_MANAGE);
            }
            else
            {
                eliminarButton.Visible = false;
            }
        }

        private void Bind()
        {
            this.lblPuntoDeVenta.Text = PuntoDeVentaCurrent.DisplayName;

            this.txtCodigo.Text = SubPuntoDeVentaCurrent.Codigo;
            this.txtTipo.Text = SubPuntoDeVentaCurrent.Tipo;
        }

        private void NuevoSubPuntoDeVenta(int pdvId)
        {
            SubPuntoDeVentaCurrent = new SubPuntoDeVenta();
            SubPuntoDeVentaCurrent.PuntoDeVentaId = PuntoDeVentaCurrent.Id;
            SubPuntoDeVentaCurrent.EmpresaId = BasePage.CompanyId;
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {                
                SetData();
                Save();
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaEdit + string.Format("?Id={1}&{0}=1", MiscClass.QueryShowSaveOk, PuntoDeVentaCurrent.Id), true);
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void Save()
        {
            SubPuntoDeVentaCurrent = WebServiceHelper.SaveSubPuntoDeVenta(SubPuntoDeVentaCurrent);
        }

        private void SetData()
        {
            SubPuntoDeVentaCurrent.Codigo = this.txtCodigo.Text;
            SubPuntoDeVentaCurrent.Tipo = this.txtTipo.Text;
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaEdit + string.Format("?Id={0}", PuntoDeVentaCurrent.Id), true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.RemoveSubPuntoDeVentaFromPdv(SubPuntoDeVentaCurrent.Id);
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaEdit + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);                            
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}